﻿/**************************************************************************
**
**  MisCip
**  ~~~~~~~~~~
**
**  FileReader.cs: 
**  ---------
**  Liest die Datei mit deren Code ein
**
**  
**************************************************************************/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace Pic_Simulator
{

    class FileReader
    {
        //Attribute
        string[] zeile;
        int idx;

        // Bool für geöffnete Datei
        public bool bFileOpened = false;

        // Array von String für Code
        public String[] CodeLines;

        // ArrayList für alle Befehle
        public ArrayList alCommands = new ArrayList();

        // Integer-Array für Befehle
        public int[] Commands;

        // ReferenceList Array
        public int[] ReferenceList;


        //Konstruktoren
        public FileReader()
        {

        }

        /*********************************************************************/
        /**   FillCodeArray
        **
        **  Einlesen der Datei ohne Kommentare in ein Array
        **
        **  Ret: void
        **
        **************************************************************************/

        public void FillCodeArray(String sFileName)
        {
            if (sFileName != null)
            {
                using (TextReader Reader = new StreamReader(sFileName, Encoding.Default))
                {
                    string sLine = String.Empty;
                    int iLength = CountLines(sFileName);
                    CodeLines = new String[iLength];
                    for (int i = 0; i <= iLength; i++)
                    {
                        sLine = Reader.ReadLine();
                        if ((sLine != null))
                        {
                            CodeLines[i] = sLine;
                        }
                    }
                }
                
                // Kommentare entfernen
                for (int i = 0; i < CodeLines.Length; i++)
                {
                    if (CodeLines[i].Contains(';'))
                    {
                        int stelle = CodeLines[i].IndexOf(';');
                        CodeLines[i] = CodeLines[i].Remove(stelle);
                    }
                }
                bFileOpened = true;
            }
        }


        /*********************************************************************/
        /**   readAllCmds
        **
        **  Einlesen Befehle
        **
        **  Ret: void
        **
        **************************************************************************/

        public void readAllCmds()
        {
            int iCmds = 0;
            ArrayList alRowNumbers = new ArrayList();
            for (int i = 0; i < CodeLines.Length; i++)
            {
                String sCmd = readCmdFromLine(i);
                if( sCmd != null )
                {
                    alCommands.Add( sCmd );
                    alRowNumbers.Add(CodeLines[i].Substring(21, 5));
                    iCmds++;
                }
            }
            Commands = new int[iCmds];
            ReferenceList = new int[iCmds];

            for (int j = 0; j < iCmds; j++)
            {
                Commands[j] = HexToDec(alCommands[j].ToString());
                ReferenceList[j] = Convert.ToInt32(alRowNumbers[j]);
            }
        }

        /*********************************************************************/
        /**   readAllCmds
        **
        **  Einlesen eines Befehls von einer Zeile
        **
        **  Ret: String
        **
        **************************************************************************/

        public String readCmdFromLine(int iInRowNumber)
        {
            String Command = null;
            if (CodeLines[iInRowNumber].Length > 0)
            {
                Command = CodeLines[iInRowNumber].Substring(5, 4);
            }

            if( Command == "    " )
            {
                Command = null;
            }
            return Command;
        }

        /*********************************************************************/
        /**   HexToDec
        **
        **  Umwandlung von Hex zu Dezimal
        **
        **  Ret: int
        **
        **************************************************************************/
        public int HexToDec(string hexValue)
        {
            if (hexValue == "")
            {
                return 0;
            }
            return Int32.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
        }

        /*********************************************************************/
        /**   CountLines
        **
        **  Ermittelt die Anzahl der Zeilen in eienr Datei
        **
        **  Ret: int
        **
        **************************************************************************/
        private static int CountLines(string fileToCount)
        {
            int counter = 0;
            using (StreamReader countReader = new StreamReader(fileToCount))
            {
                while (countReader.ReadLine() != null)
                {
                    counter++;
                }
            }
            return counter;
        }
    }
}
